package com.lg.online_shop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.online_shop.entity.Good;
import com.lg.online_shop.vo.CommonChartData;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface GoodMapper extends BaseMapper<Good> {

    // 获取推荐商品
    @Select("SELECT * FROM good WHERE g_category_id = #{categoryId} ORDER BY g_price ")
    IPage<Good> selectGoodsByCategoryWithPagination(@Param("categoryId") Long categoryId, @Param("page") Page<Good> page);

    // 获取最受欢迎列表
    @Select("SELECT * FROM good WHERE g_category_id = #{categoryId} ORDER BY g_star DESC")
    IPage<Good> selectPopularGoodsByCategory(@Param("page") Page<Good> page, @Param("categoryId") Long categoryId);

    // 根据关键字和字段搜索商品
    @Select("SELECT * FROM good WHERE g_name LIKE concat('%',#{keyword},'%') OR good.g_desc LIKE concat('%',#{keyword},'%')")
    IPage<Good> selectPageByKeywordAndFields(@Param("page") Page<Good> page, @Param("keyword") String keyword);

    @Select("SELECT \n" +
            "    good_category.title as name, \n" +
            "   SUM(good.g_stock)  as value\n" +
            "FROM \n" +
            "    good \n" +
            "JOIN \n" +
            "    good_category ON good.g_category_id = good_category.id \n" +
            "GROUP BY \n" +
            "    good_category.title;")
    List<CommonChartData> selectCountWithGood();
}